package cc.owoo.godpen.analysis.css;

import cc.owoo.godpen.analysis.StringAnalysis;

import java.util.LinkedList;
import java.util.Locale;

/**
 * 样式表
 * Created by nimensei
 * 2022-05-30 下午 10:14
 **/
public class Style {
    private final LinkedList<SelectorProperty> selectorProperties = new LinkedList<>();// 样式

    /**
     * 解析样式表
     *
     * @param string css样式表
     * @return 样式表对象
     */
    public static Style parse(String string) {
        Style style = new Style();
        StyleStringAnalysis analysis = new StyleStringAnalysis(string.toCharArray());
        Media media = null;
        while (analysis.hashNext()) {
            Character character = analysis.nextNotNull(null);
            if (character == null)
                break;
            char c = character;
            if (c == '{') {
                analysis.skipBracket(1);
                continue;
            }
            if (c == '}') {
                if (media != null && media.size() > 0)
                    style.add(media);
                media = null;
                continue;
            }
            if (c == '/') {
                if (analysis.next() == '*')
                    analysis.nextIndexOf("*/".toCharArray());
                continue;
            }
            if (c == '@') {
                String name = analysis.nextName();
                if (name == null)
                    continue;
                name = name.toLowerCase(Locale.ROOT);
                if (name.equals("keyframes")) {
                    Keyframes keyframes = parseKeyframes(analysis);
                    (media != null ? media : style).add(keyframes);
                } else if (name.equals("media")) {
                    if (media != null) {
                        analysis.skipBracket(0);
                        continue;
                    }
                    media = parseMedia(analysis);
                }
                continue;
            }
            analysis.reset();
            SelectorGroup selectorGroup = analysis.nextSelectorGroup('{');
            if (selectorGroup == null)
                break;
            Property property = analysis.nextProperty('}');
            if (property == null)
                break;
            if (selectorGroup.size() == 0 || property.size() == 0)
                continue;
            (media != null ? media : style).add(selectorGroup, property);
        }
        return style;
    }

    /**
     * 解析帧动画
     *
     * @param analysis 帧动画对象
     */
    private static Keyframes parseKeyframes(StringAnalysis analysis) {
        Keyframes keyframes = new Keyframes();
        return keyframes;
    }

    /**
     * 解析媒体查询
     *
     * @param analysis 字符串解析对象
     */
    private static Media parseMedia(StringAnalysis analysis) {
        Media media = new Media();
        return media;
    }

    /**
     * 添加样式
     *
     * @param selector 选择器
     * @param property css属性
     */
    public void add(Selector selector, Property property) {
        if (selector == null || property == null)
            return;
        selectorProperties.add(new SelectorProperty(selector, property));
    }

    /**
     * 添加帧动画
     *
     * @param keyframes 帧动画对象
     */
    public void add(Keyframes keyframes) {

    }

    /**
     * 添加媒体查询
     *
     * @param media 媒体查询对象
     */
    public void add(Media media) {

    }

    /**
     * 序列化样式表
     *
     * @param string  序列化字符串
     * @param retract 缩进
     * @param format  是否格式化代码
     */
    public void stringify(StringBuilder string, String retract, boolean format) {
        if (string == null)
            return;
        for (SelectorProperty selectorProperty : selectorProperties)
            selectorProperty.stringify(string, retract, format);
    }

    /**
     * 获取样式元素数量，计算方式为样式表、帧动画、媒体查询的合
     *
     * @return 样式元素数量
     */
    public int size() {
        return selectorProperties.size();
    }

    /**
     * 将样式表序列化
     *
     * @return 序列化后的字符串
     */
    public String stringify() {
        StringBuilder string = new StringBuilder();
        stringify(string, null, false);
        return string.toString();
    }

    /**
     * 将样式表序列化，并进行格式化处理
     *
     * @return 序列化后的字符串
     */
    public String stringifyFormat() {
        StringBuilder string = new StringBuilder();
        stringify(string, null, true);
        return string.toString();
    }

    @Override
    public String toString() {
        return stringify();
    }
}
